﻿using HECore.Account.Contract;
using HECore.Account.Contract.Models.Account.Dtos;
using HECore.Account.Contract.Models.Account.Inputs;
using HECore.Account.Repository.Context;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HECore.ServiceFrame.Filters;
using HECore.Common.Extensions;

namespace HECore.Account.Service
{
    /// <summary>
    /// 账号服务
    /// </summary>
    public class AccountService : IAccountService
    {
        private readonly AccountContext _accountContext;
        public AccountService(AccountContext accountContext)
        {
            _accountContext = accountContext;
        }
        
        public async Task<AccountDto> Login(AccountLoginInput input)
        {
            var account = await _accountContext.Account.AsNoTracking().Where(x => x.Password == input.Password && (x.Mobile == input.Account || x.Email == input.Account)).FirstOrDefaultAsync();
            if (account != null)
            {
                return account.MapTo<AccountDto>();
            }
            else
            {
                throw new UserFriendlyException("passwor.error", "登录名或密码不正确");
            }
        }
    }
}
